<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="750px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto">
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="公司名称" prop="compName">
              <el-input v-model="ruleForm.compName" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司简称" prop="shortName">
              <el-input v-model="ruleForm.shortName" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司编码" prop="compNo">
              <el-input v-model="ruleForm.compNo" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管理员账号" prop="account">
              <el-input v-model.trim="ruleForm.account" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管理员密码" prop="password">
              <el-input v-model.trim="ruleForm.password" type="password" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="linkMan">
              <el-input v-model="ruleForm.linkMan" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="mobile">
              <el-input v-model="ruleForm.mobile" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="公司网址" prop="webUrl">
              <el-input v-model="ruleForm.webUrl">
                <template slot="prepend">Http://</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="公司地址" prop="address">
              <el-input v-model="ruleForm.address" maxlength="100" type="textarea" rows="2" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="ruleForm.remark" maxlength="200" type="textarea" rows="3" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
    <!-- <span slot="footer">
      <el-button type="info" @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    </span> -->
  </el-dialog>
</template>
<script>
export default {
  data() {
    const checkName = async(rule, value, callback) => {
      const res = await this.$axios.get(`/platform/comp/verifyNameUnique?account=${value}&id=${this.ruleForm.accountUserId || ''}`)
      if (res?.data.code === 200) {
        return res.data.data ? callback() : new Error(callback('管理员账号重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    const checkCode = async(rule, value, callback) => {
      const res = await this.$axios.get(`/platform/comp/verifyCompNoUnique?compNo=${value}&id=${this.ruleForm.id || ''}`)
      if (res?.data.code === 200) {
        return res.data.data ? callback() : new Error(callback('公司编码重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      deptList: [],
      ruleForm: {
        compName: '',
        shortName: '',
        compNo: '',
        account: '',
        password: '',
        linkMan: '',
        mobile: '',
        webUrl: '',
        remark: ''
      },
      rules: {
        compName: [{ required: true, message: 'This field cannot be empty!', trigger: 'blur' }],
        shortName: [{ required: true, message: 'This field cannot be empty!', trigger: 'blur' }],
        compNo: [
          { required: true, message: 'This field cannot be empty!', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ],
        account: [
          { required: true, message: 'This field cannot be empty!', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
          // { min: 6, message: '账号长度不能低于6位！', trigger: 'blur' }
        ],
        password: [
          { required: true, message: 'This field cannot be empty!', trigger: 'blur' },
          { min: 6, message: '密码长度不能低于6位！', trigger: 'blur' }
        ],
        linkMan: [{ required: false }],
        mobile: [{ required: false }],
        webUrl: [{ required: false }],
        remark: [{ required: false }]
      },
      dialogVisible: false,
      departData: []
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        const res = await this.$axios.get(`/platform/comp/detail?id=${row.id}`)
        console.log(res)
        if (res.data.data.hasOwnProperty('id')) {
          this.ruleForm = res.data.data
          this.dialogVisible = true
        } else {
          this.$notify.warning('接口数据异常！')
        }
      } else {
        this.title = '新增'
        this.dialogVisible = true
      }
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          let url = '/platform/comp/insert'
          if (this.ruleForm.hasOwnProperty('id')) {
            url = '/platform/comp/update'
          }
          const res = await this.$axios.post(url, this.ruleForm)
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        compName: '',
        shortName: '',
        account: '',
        password: '',
        linkMan: '',
        mobile: '',
        webUrl: '',
        remark: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
